﻿Imports System.IO

Public Class QuanLyThucDon
    Dim strFileNameAndPath As String = ""
    Private Sub QuanLyThucDon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Loaimon()
    End Sub
    Sub Load_Loaimon()
        Dim list As List(Of CLoaiMon) = LoaiMonDao.LayDanhSach()
        cboMon.DataSource = list
        cboMon.DisplayMember = "TenLoai"
    End Sub

    Private Sub btnThemMon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemMon.Click
        Dim m As New CMon

        If strFileNameAndPath = "" Then
            MsgBox("Ban chua chon file")
        Else
          
            m.MaLoai = CType(cboMon.SelectedItem, CLoaiMon).MaLoai
            m.TenMon = txtTenMon.Text
            m.DonGia = CType(txtDG.Text, Integer)
            m.ImagePath = System.IO.Path.GetFileName(strFileNameAndPath)
            If System.IO.File.Exists(strFileNameAndPath) Then
                Dim strDir As String = Environment.CurrentDirectory

                File.Copy(strFileNameAndPath, strDir + "\\" + m.ImagePath)
                Dim kq As Integer = MonDAO.Insert(m)
                If kq > 0 Then
                    MsgBox("Them mon Thanh cong")
                    cboMon_SelectedIndexChanged(sender, e)
                Else
                    MsgBox("Them mon That bai")
                End If

            End If




        End If
        '.ImagePath


    End Sub

    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        strFileNameAndPath = OpenFile()

        'check to see if they selected a file or just clicked cancel
        If (strFileNameAndPath = "") Then
            'Chastise the user for not selecting a file :)
            MsgBox("Ban ko chon file")
        Else
            Try
                txtHinh.Text = strFileNameAndPath
                PictureBox1.Image = New Bitmap(strFileNameAndPath)

            Catch ex As Exception

            End Try

        End If
    End Sub
    Public Function OpenFile() As String

        Dim strFileName = ""  'declare a new open file dialog
        Dim fileDialogBox As New OpenFileDialog()


        fileDialogBox.Filter = "Image Files (*.bmp;*.jpg;*.gif)|*.bmp;*.jpg;*.gif"


        fileDialogBox.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal)


        If (fileDialogBox.ShowDialog() = DialogResult.OK) Then

            strFileName = fileDialogBox.FileName


        End If
        Return strFileName
    End Function

    Private Sub cboMon_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboMon.SelectedIndexChanged
        Try
            Dim c As CLoaiMon = CType(cboMon.SelectedItem, CLoaiMon)
            load_Mon(c.MaLoai)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
       
    End Sub
    Sub load_Mon(ByVal maloai As Integer)
        lvwTD.Items.Clear()
        Dim list As List(Of CMon) = MonDAO.LayDanhSach(maloai)

        For Each i As CMon In list
            Dim l As New ListViewItem(i.TenMon)
            l.SubItems.Add(i.DonGia)
            l.SubItems.Add(i.MaLoai)
            l.SubItems.Add(i.ImagePath)
            l.Tag = i
            lvwTD.Items.Add(l)
        Next

    End Sub

    Private Sub btnLuuMon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuuMon.Click
        If lvwTD.SelectedItems.Count < 0 Then
            MsgBox("Chua chon Mon")
        Else
            Dim c As CMon = CType(lvwTD.SelectedItems(0).Tag, CMon)
            Dim k As Integer = MonDAO.Delete(c.MaMon)
            If k > 0 Then
                MsgBox("Xoa Thanh Cong")
                Dim strDir As String = Environment.CurrentDirectory
                File.Delete(strDir + "\\" + c.ImagePath)
                cboMon_SelectedIndexChanged(sender, e)
            Else

            End If

        End If
    End Sub
End Class